<template>
  <div>
    <el-dialog
      :title="isEdit ? '编辑子部门' : '新增子部门'"
      :visible.sync="dialogVisible"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="dialogCloseFn"
    >
      <div>
        <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="form.name" style="width:80%" placeholder="请输入部门名称" />
          </el-form-item>
          <el-form-item label="部门编码" prop="code">
            <el-input v-model="form.code" style="width:80%" placeholder="请输入部门编码" />
          </el-form-item>
          <el-form-item label="部门负责人" prop="manager">
            <el-select v-model="form.manager" style="width:80%" placeholder="请选择部门负责人">
              <el-option v-for="item in employeeSimpleList" :key="item.id" :value="item.username" />
            </el-select>
          </el-form-item>
          <el-form-item label="部门介绍" prop="introduce">
            <el-input v-model="form.introduce" style="width:80%" placeholder="请输入部门介绍" type="textarea" :rows="3" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="enterFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    employeeSimpleList: {
      type: Array,
      default: () => []
    },
    originList: {
      type: Array,
      default: () => []
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    currentDepartId: {
      type: String,
      default: ''
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map(item => item.code)
      // 如果是编辑状态，则将需要编辑的这一项排除在外
      if (this.isEdit) {
        existCodeList = this.originList.filter(item => item.id !== this.currentDepartId).map(item => item.code)
      }
      existCodeList.includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
    }

    const validName = (rule, value, callback) => {
      // 新增找下一级
      let existNameList = this.originList.filter(item => item.pid === this.currentDepartId).map(item => item.name)
      // 如果是编辑的情况，找同一级
      if (this.isEdit) {
        // 找到部门的当前对象
        const dept = this.originList.find(item => item.id === this.currentDepartId)
        // 取出父级的 id (当前对象的pid)
        const pid = dept.pid
        // 过滤，筛选出同一级别的数据, 但是id不包含当前编辑部门的id, 映射返回name名字数组
        existNameList = this.originList
          .filter(item => item.pid === pid && item.id !== this.currentDepartId)
          .map(item => item.name)
      }
      existNameList.includes(value) ? callback(new Error('部门名字' + value + '已经存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 取消-点击事件
    cancelFn() {
      this.$emit('update:dialogVisible', false)
    },
    // 确定-点击事件
    async enterFn() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          this.$emit('addDepartEV', { ...this.form })
          this.$emit('update:dialogVisible', false)
        } else {
          return false
        }
      })
    },
    dialogCloseFn() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
    }
  }
}
</script>

<style>
.el-dialog__footer {
  text-align: center;
}
</style>
